package com.example.adminvue.controller;

import com.example.adminvue.domain.AjaxResult;
import com.example.adminvue.domain.BInfo;
import com.example.adminvue.domain.BKind;
import com.example.adminvue.domain.TableDataInfo;
import com.example.adminvue.service.BInfoService;
import com.example.adminvue.service.BKindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/web/info")
public class BInfoController extends BaseController
{
    @Autowired
    private BInfoService bInfoService;

    @Autowired
    private BKindService bKindService;

    @GetMapping("/list")
    public TableDataInfo list(BInfo info)
    {
        startPage();
        List<BInfo> list = bInfoService.selectList(info);
        TableDataInfo rspData = getDataTable(list);
        List<BInfo> rows = (List<BInfo>) rspData.getRows();
        List<BKind> kindList = bKindService.selectList(new BKind());
        for (BInfo f : rows) {
            if(f.getInfoKind()!=null && f.getInfoKind()>0){
                BKind bKind = kindList.stream().filter(t -> t.getId().equals(f.getInfoKind())).findFirst().get();
                f.setInfoKindName(bKind.getKindName());
            }
            else{
                f.setInfoKindName("主类目");
            }


        }
        return rspData;
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return AjaxResult.success(bInfoService.selectById(id));
    }

    @PostMapping
    public AjaxResult add(@Validated @RequestBody BInfo info)
    {
        return toAjax(bInfoService.insert(info));
    }

    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BInfo info)
    {
        return toAjax(bInfoService.update(info));
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(bInfoService.deleteById(id));
    }
}
